package com.visionblue.business.ship.page.outer

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.visionblue.business.ship.base.BaseViewModel
import com.visionblue.business.ship.base.ViewState
import com.visionblue.business.ship.ext.SingleLiveEvent
import com.visionblue.business.ship.model.Results
import com.visionblue.business.ship.model.bean.response.ApplicationDetail
import com.visionblue.business.ship.model.bean.response.ListBean
import com.visionblue.business.ship.model.bean.response.TransferOuterDetail
import com.visionblue.business.ship.model.bean.response.TransportShipInfo
import com.visionblue.business.ship.page.MainRepository
import com.visionblue.business.ship.page.applications.AbsApplicationsViewModel
import kotlinx.coroutines.launch

/**
 * @author: xlh
 * @date: 2020-06-08 8:03 AM
 * @desc:
 */
class OuterViewModel(
    private val outerRepository: OuterRepository
) : BaseViewModel() {

    val transferOuterViewState = MutableLiveData<ViewState<ListBean<ApplicationDetail>>>()
    val operateTaskEvent = SingleLiveEvent<ViewState<Any>>()
    val transporters = MutableLiveData<ViewState<List<TransportShipInfo>>>()

    //  任务列表当前页码
    var currPage: Int = PAGE_START
    var totalElements: Int = -1

    val simpleEvent = SingleLiveEvent<ViewState<Any>>()

    fun getTransferOutTasks() {
        viewModelScope.launch {
            checkState(transferOuterViewState, Results.Loading)
            checkState(
                transferOuterViewState,
                outerRepository.getTransferOutTasks(currPage),
                successAction = { listBean, b ->
                    totalElements = listBean?.totalElements ?: 0
                    transferOuterViewState.value =
                        ViewState(data = listBean, isSuccess = true, isLoadMore = b)
                })
        }
    }

    fun prePage() {
        currPage--
        getTransferOutTasks()
    }

    fun nextPage() {
        currPage++
        getTransferOutTasks()
    }

    fun resetPageNum() {
        currPage = PAGE_START
    }

    fun recallOut(id: String?) {
        if (id == null) return
        viewModelScope.launch {
            checkState(operateTaskEvent, Results.Loading)
            checkState(operateTaskEvent, outerRepository.cancelOwnApplication(id))
        }
    }

    fun getApplicationsAfterOperation() {
        if (currPage >= totalElements) currPage--
        if (currPage < 1) {
            currPage = 1
        }
        getTransferOutTasks()
    }

    fun getTransporters() {
        viewModelScope.launch {
            checkState(transporters, Results.Loading)
            checkState(transporters, outerRepository.getTransporters())
        }
    }

    fun createTransportApplication(shipNo: String?) {
        if (shipNo.isNullOrEmpty()) return
        viewModelScope.launch {
            checkState(simpleEvent, Results.Loading)
            checkState(simpleEvent, outerRepository.postTransportApplication(shipNo))
        }
    }

    companion object {
        private const val PAGE_START = 1
    }
}